<?php
namespace app\cms\model;
/* *
 * 内容管理
 * */
class index extends \app\base\model\AdminModel{
    protected $table = 'cms_content';  //文章表

    //查询
    public function _select($cid,$website_id){
        $all = model('channel')->AllChildClass($cid,$website_id);
        $condition[] = 'channel_id in('.$all.')';
        return $this->table('cms_content as A')
            ->join('{pre}cms_class as B ON A.channel_id = B.id')
            ->field('A.channel_id,A.title,A.id,A.uptime,A.views,A.sort,A.status,A.fontcolor,B.name as channel_name')
            ->where($condition)
            ->order('a.sort asc,a.id desc')
            ->select();
    }

    //单个
    public function _find($condition,$field = NULL){
        return $this->where($condition)->field($field)->find();
    }

    //单个+扩展查询
    public function _finds($uid){
        $rel =  $this->where(array('id'=>$uid))->field('id,channel_id,title,url,uptime,editor,fontcolor,uptime,views')->find();
        if($rel){
            $this->where(array('id'=>$uid))->setInc('views');
            $lists = model('channel')->_finds($rel['channel_id'],'id,website_id,name,url,uptime,tpl_content,tpl_list,exts_mid,mid');
            $tables = model('module')->tables($lists['mid']);
            $rel_ext = $this->table($tables)->where(array('content_id'=>$uid,'channel_id'=>$lists['id']))->find();
            if($rel_ext){
                $rel_ext = filter_string($rel_ext,array('channel_id','content_id'),1);
                $rel = array_merge($rel,$rel_ext);
            }
            if(empty($rel['url'])){
              $rel['url'] = url('cms/default/read',array('uid'=>$rel['id']));
            }
            $str[0] = $lists;
            $str[1] = $rel;
        }
        return $str;
    }

    //删除
    public function _delete($fid){
        //查询所有关联表
        $info = $this->_find(array('id'=>$fid),'channel_id');
        $tables = model('channel')->_model($info['channel_id']);
        $channel = model('channel')->_find(array('id'=>$info['channel_id']),'plus_mid');
        //删除
        $rel = $this->where(array('id'=>$fid,'channel_id' =>$info['channel_id']))->delete();
        if($rel){
            $result = $this->table($tables)->where(array('content_id'=>$fid,'channel_id' =>$info['channel_id']))->delete();
            if($channel['plus_mid']){
                $ext_tables = model('module')->tables($channel['plus_mid']);
                $ext_result = $this->table($ext_tables)->where(array('content_id'=>$fid,'channel_id' =>$info['channel_id']))->delete();
            }   
        }
        if($result){
            return TRUE;
        }else{
            return FALSE;
        }   
    }

    //编辑
    public function _update($data,$condition){
        return $this->where($condition)->data($data)->update();
    } 

   //编辑
   public function _edit($fid){
        $fid = intval($fid);
        $info = $this->where(array('id'=>$fid))->find();
        if($info['channel_id']){
            $tables = model('channel')->_model($info['channel_id']);
            $ext_info = $this->table($tables)->where(array('content_id'=>$fid))->find();
            if($ext_info){
                return array_merge($info,$ext_info);
            }else{
                return FALSE;
            }

        }else{
            return FALSE;
        }    
   }

   //增加
   public function dataSave($mid,$postinput,$fid = null){
        $data['channel_id'] = $postinput['channel_id'];
        $data['title'] = trim($postinput['title']);
        $data['sort'] = trim($postinput['sort']);
        $data['views'] = trim($postinput['views']);
        $data['url'] = trim($postinput['url']);
        $data['uptime'] = $postinput['uptime'];
        $data['editor'] = trim($postinput['editor']);
        $data['fontcolor'] = trim($postinput['fontcolor']);
        $data['status'] = 1;
        if($postinput['special']){
            $data['special'] = implode(',', $postinput['special']);
        }
        $mid  = intval($mid);
        $table = model('module')->tables($mid);
        $field = model('field')->_select(array('mid'=>$mid),'neworder asc,id desc','fields');
        foreach ($field as $value) {
           $ext_data[$value['fields']] = trim($postinput[$value['fields']]);;
        }
        if($fid){
            $condition['id'] = $fid;
            $rel = $this->where($condition)->data($data)->update();
            if($rel){
                $ext_data['channel_id'] = $postinput['channel_id'];
                $result = $this->table($table)->where(array('content_id'=>$fid))->data($ext_data)->update();
            }       
        }else{
            $rel = $this->data($data)->insert();
            if($rel){
                $ext_data['content_id'] = $rel;
                $ext_data['channel_id'] = $postinput['channel_id'];
                $result = $this->table($table)->data($ext_data)->insert();
            }
        }
        return $result ;
    }
    //###########################################
    //判断是否有扩展
    public function exts_Count($cid,$fid){
        $cid = intval($cid);
        $fid = intval($fid);
        $mid = model('channel')->_find(array('id'=>$cid),'plus_mid');
        if($mid['plus_mid']){
            $tables = model('module')->tables($mid['plus_mid']);
            $count = $this->table($tables)->where(array('content_id' =>$fid,'channel_id' =>$cid))->count();
            echo ' <a class="red" href="'.url('index/exts',array('mid'=>$rel,'cid'=>$cid,'id'=>$fid)).'">[ '.$count.' ]</a>';
        }
    }

    //扩展内容列表
    public function exts_select($cid,$id){
        $mid = model('channel')->_find(array('id'=>$cid),'plus_mid');
        $table = model('module')->tables($mid['plus_mid']);
        $rel = $this->table($table)->where(array('content_id' =>$id,'channel_id' =>$cid))->field('id,content_id,channel_id,title,uptime,fontcolor,views,sort,url')->order('id desc')->select();
        return $rel;
    }

   //编辑扩展内容
   public function extsedit($mid,$fid){
        $tables = model('module')->tables(intval($mid));
        return $this->table($tables)->where(array('id'=>intval($fid)))->find();
    }

    //扩展内容保存
    public function extssave($postinput,$channel_id,$content_id,$fid = NULL){
        $channel = model('channel')->_find(array('id'=>$channel_id),'plus_mid,id');
        $table = model('module')->tables($channel['plus_mid']);
        $data = $postinput;
        $data['uptime'] = time();
        if($fid){
             return $this->table($table)->where(array('id' => $fid))->data($data)->update();
        }else{
             return $this->table($table)->data($data)->insert();
        }
   }

    //删除文章
   public function extsdelete($cid,$fid){
        $channel = model('channel')->_find(array('id'=>$cid),'plus_mid,id');
        $table = model('module')->tables($channel['plus_mid']);
        return $this->table($table)->where(array('id' => $fid))->delete();
   }


   //##########################################################
    //前台栏目标签算法
    public function Label($labes,$ClassID,$page = NULL){
        //是否包含子类
        if($labes['es_subclass']){
            $channel = ' and '.$labes['es_class'].' in ('.model('channel')->AllChildClass($ClassID).')';
        }else{
            $channel = ' and '.$labes['es_class'].' = '.$ClassID;
        }
        //资源特性
        if(!empty($labes['es_special'])){
            $es_special = str_replace(',','\',\'', $labes['es_special']);
            $es_special = '\''.$es_special.'\'';
            $special = ' and find_in_set('.$es_special.',special)';
        }
        //自定义排序
        $order = empty($labes['es_order'])?'id DESC':$labes['es_order'];

        //每页多少
        $limits = empty($labes['es_limits'])?'20':$labes['es_limits'];
        $StartPage = empty($page)?0:form('get.page',0,intval);

        //是否有自定义SQL
        if(!empty($labes['es_where']))$where = ' and '.$labes['es_where'];

        //是否支持搜索
        if($labes['es_search'] == 1){
            $wd = form('get.wd');
            if($wd){
                $search = ' and title like \'%'.$wd.'%\'';
            }
        }

        //拼凑SQL 
        $sql = array(0 => 'status = 1'.$channel.$special.$search.$where);
        $result = $this->table('cms_content as A')
            ->join('{pre}cms_class as B ON A.channel_id = B.id')
            ->field('A.*,B.name,B.mid')
            ->pager($StartPage,$limits)
            ->where($sql)
            ->order($order)
            ->select();
        if($result){
            foreach ($result as $key => $value) {
                if(empty($value['url'])){
                  $value['url'] = url('cms/default/read',array('uid'=>$value['id']));
                }
                if($value['mid']){
                    $tables = model('module','cms')->tables($value['mid']);
                    $rel = $this->table($tables)->where(array('content_id'=>$value['id'],'channel_id'=>$value['channel_id']))->find();
                    if($rel){
                      $str = filter_string($rel,array('content_id','channel_id'));
                      $value = filter_string($value,array('sort','special','mid','status'));
                      $ary[0][$key] = array_merge($value,$str);
                    }
                }else{
                    $ary[0][$key] = $value;
                }
            }
            if($page){
                $ary[1] = getPage($this->pager,array('uid'=>$ClassID));
            }
            return $ary;
        }else{
            return array();
        }
   }
}